import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/utils/utils.dart';

import '../base/qkd_text.dart';

enum ErrorType{
  none(pic: "",tip: ""),
  noData(pic: "match_no_data.png", tip: "暂无数据"),
  network(pic: "error_network.png", tip: "网络不稳定，请检查网络"),
  other(pic: "", tip: "");

  final String pic;
  final String tip;

  const ErrorType({required this.pic, required this.tip});
}

class NoDataWidget extends StatelessWidget {
  final String? tip;
  final Function()? onTap;
  final String buttonText;
  final bool needScroll;
  final bool needPic;
  final ScrollPhysics? physics;
  final double? height;
  final bool isSliver;
  final double? picSize;
  final ErrorType type;

  const NoDataWidget(
    {super.key,
    this.buttonText = "",
    this.onTap,
    this.tip,
    this.needScroll = false,
    this.physics,
    this.needPic = true,
    this.height,
    this.picSize,
    this.type = ErrorType.noData,
    this.isSliver = false
    });

  const NoDataWidget.sliver(
    {super.key,
      this.tip,
      this.onTap,
      this.buttonText = "",
      this.needScroll = false,
      this.needPic = true,
      this.physics,
      this.height,
      this.picSize,
      this.type = ErrorType.noData
    }):isSliver = true;

  @override
  Widget build(BuildContext context) {
    if(needScroll){
      return SizedBox.expand(
        child: LayoutBuilder(builder: (p0, p1) {
          log(p1.maxHeight.toString());
          return SingleChildScrollView(
            physics: physics ?? const ScrollPhysics(),
            child: Container(
              height: height ?? p1.maxHeight,
              alignment: Alignment.center,
              child: noData(),
            ),
          );
        }),
      );
    }else if(isSliver){
      return SliverLayoutBuilder(
        builder: (BuildContext context, SliverConstraints cons) {
          return SliverToBoxAdapter(
            child: Container(
              constraints: const BoxConstraints(minHeight: 200),
              height: height ?? cons.viewportMainAxisExtent,
              alignment: Alignment.center,
              child: noData(),
            ),
          );
        }
      );
    }else{
      return noData();
    }
  }

  Widget noData() {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (needPic)
            Image.asset(
              Utils.getImgPath(type.pic),
              width: picSize ?? 160,
            ).paddingOnly(bottom: 10),
          Container(
            height: !needPic && height != null ? height : null,
            alignment: Alignment.center,
            child: QkdText(tip ?? type.tip, color: Colours.grey66),
          ),
          if (buttonText.isNotEmpty && onTap != null)
            GestureDetector(
              onTap: onTap,
              child: Container(
                padding:
                  const EdgeInsets.symmetric(horizontal: 16, vertical: 5),
                margin: const EdgeInsets.only(top: 14),
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.all(Radius.circular(6)),
                  border: Border.all(color: Colours.main, width: 0.5),
                ),
                child: QkdText(
                  buttonText,
                  fontWeight: FontWeight.w400,
                  color: Colours.main,
                ),
              ),
            )
        ],
      ),
    );
  }
}
